package com.jeeplus.modules.common.excel.common.validator.impl;

import com.jeeplus.common.utils.StringUtil;
import com.jeeplus.modules.api.major.entity.Major;
import com.jeeplus.modules.api.major.service.MajorService;
import com.jeeplus.modules.api.majorResearch.entity.MajorResearch;
import com.jeeplus.modules.api.majorResearch.service.MajorResearchService;
import com.jeeplus.modules.common.excel.common.anno.ResearchName;
import com.jeeplus.modules.common.excel.common.validator.abs.AbstractValidator;
import com.jeeplus.modules.common.utils.SpringBeanUtils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 研究方向名称校验
 */
public class ResearchNameValidator  extends AbstractValidator {

    private final static String NOTE_RESEARCH_KEY = "noteResearchKey";

    private final static String majorKey = "majorName";

    private ResearchName researchName;

    private MajorService majorService;

    private MajorResearchService majorResearchService;

    @Override
    public boolean canValidate(Field field) {
        this.researchName = field.getDeclaredAnnotation(ResearchName.class);
        return this.researchName != null;
    }

    @Override
    public String toValidate(Object entity, Field field) {
        Map<String, List<Object>> necessaryValidateData = this.chain.getNecessaryValidateData();
        List<Object> noteData = null;
        List<Object> majorData = null;
        if (!necessaryValidateData.containsKey(NOTE_RESEARCH_KEY)) {
            majorResearchService = SpringBeanUtils.getBean(MajorResearchService.class);
            noteData = new ArrayList<>();
            MajorResearch majorResearchParam = new MajorResearch();
            majorResearchParam.setType("2");//研究方向
            noteData = Collections.singletonList(majorResearchService.findList(majorResearchParam));
            necessaryValidateData.put(NOTE_RESEARCH_KEY, noteData);
        } else {
            noteData = necessaryValidateData.get(NOTE_RESEARCH_KEY);
        }
        if (!necessaryValidateData.containsKey(majorKey)) {
            majorData = new ArrayList<>();
            majorService = SpringBeanUtils.getBean(MajorService.class);
            Major majorParam = new Major();
            majorParam.setGradation("1,2,3,4,5");//层次：1本科,4专升本,5第二学位:
            List<Major> majorList = majorService.findList(majorParam);
            if (majorList != null && majorList.size() > 0) majorData.addAll(majorList);
            necessaryValidateData.put(majorKey, majorData);
        } else {
            majorData = necessaryValidateData.get(majorKey);
        }
        String transformTo = this.researchName.transformTo();
        field.setAccessible(true);
        try {
            Object value = field.get(entity);
            if (value == null) return null;
            if (value instanceof String) {
                String noteNameStr = value.toString();
                String majorNameStr = null;
                String forMajor = this.researchName.forMajor();
                String majorId = null;
                MajorResearch majorResearch = null;
                if (StringUtil.isNotBlank(forMajor)) {
                    Field forMajorField = entity.getClass().getDeclaredField(forMajor);
                    forMajorField.setAccessible(true);
                    majorNameStr = (String) forMajorField.get(entity);
                    if (StringUtil.isNotBlank(majorNameStr)) {
                        String finalMajorNameStr = majorNameStr;
                        List<Object> majorList = majorData.stream().filter(major -> ((Major) major).getMajorName().equals(finalMajorNameStr)).collect(Collectors.toList());
                        if (majorList.size() > 0) {
                            Major major = (Major) majorList.get(0);
                            majorId = major.getId();
                        }
                    }
                }
                String finalMajorId = majorId;
                List<Object> noteList = noteData.stream().filter(noteResearch ->
                        ((MajorResearch) noteResearch).getName().equals(noteNameStr) &&
                                (!StringUtil.isNotBlank(finalMajorId) || ((MajorResearch) noteResearch).getMajorId().equals(finalMajorId))).collect(Collectors.toList());
                if (noteList.size() == 0) {
                    return this.researchName.msg();
                } else {
                    majorResearch = (MajorResearch) noteList.get(0);
                }
                if (StringUtil.isNotBlank(transformTo)) {
                    Field transformToField = entity.getClass().getDeclaredField(transformTo);
                    transformToField.setAccessible(true);
                    transformToField.set(entity, majorResearch.getId());
                }
            } else {
                return "研究方向值数据类型错误！";
            }
        } catch (IllegalAccessException | NoSuchFieldException e) {
            e.printStackTrace();
        }
        return null;
    }
}
